import { defineStore } from 'pinia'

export const useCounterStore = defineStore('counter', {
  state: () => ({
    count: 0
  }),
  getters: {
    double() {
      return this.count * 2
    },
    replaceTel() {
      const number = useNumberStore()
      return number.replaceTel
    },
    showNum () {
      const number = useNumberStore()
      return number.num + this.count
    }
  },
  actions: {
    increment() {
      this.count++
    },
    logCount() {
      this.extraFun()
    },
    extraFun() {
      console.log(this.count)
    },
    increNum() {
      const number = useNumberStore()
      number.addNum()
    }
  }
})

export const useNumberStore = defineStore('number', {
  state: () => ({
    num: 0,
    tel: '17700000000'
  }),
  getters: {
    replaceTel: state => state.tel.replace(
      /\d{4}(?=\d{4}$)/g,
      Array(4)
        .fill('*')
        .join('')
    )
  },
  actions: {
    addNum() {
      this.num += 20
    }
  }
})
